package com.qms.repast.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.qms.repast.R;
import com.qms.repast.commons.GlideApp;
import com.qms.repast.entity.resbean.CuisineBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 泅渡者
 * @date 2018/8/9 0009
 * @describe: 点菜卡片适配器
 */
public class CardViewAdapter extends RecyclerView.Adapter<CardViewAdapter.ViewHolder> {

	private Context mContext;
	private List<CuisineBean> cardBeans;
	private OnItemClickListener mItemClickListener;

	/**
	 * 点击事件
	 */
	public interface OnItemClickListener {
		/**
		 * 点击
		 *
		 * @param position
		 * @param item
		 */
		void onItemClick (int position, CuisineBean item);
	}

	public void setmItemClickListener (OnItemClickListener mItemClickListener) {
		this.mItemClickListener = mItemClickListener;
	}

	public CardViewAdapter (Context mContext) {
		this.mContext = mContext;
		cardBeans = new ArrayList<>();
	}

	@NonNull
	@Override
	public ViewHolder onCreateViewHolder (@NonNull ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(mContext).inflate(R.layout.layout_cardview_item, parent, false);
		return new ViewHolder(view);
	}

	@Override
	public void onBindViewHolder (@NonNull ViewHolder holder, final int position) {
		final CuisineBean item = cardBeans.get(position);
		GlideApp.with(mContext)
				.load(item.getIcon())
				.placeholder(R.mipmap.default_pic)
				.apply(new RequestOptions()
							   .transforms(new CenterCrop(), new RoundedCorners(15)
							   ))
				.into(holder.image);
		holder.tvName.setText(item.getTitle());
		holder.tvPrice.setText(Double.toString(item.getPrice()));
		holder.image.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick (View v) {
				mItemClickListener.onItemClick(position, item);
			}
		});
	}

	@Override
	public int getItemCount () {
		return cardBeans.size();
	}

	/**
	 * 获取当前卡片对于的菜
	 *
	 * @param position
	 *
	 * @return
	 */
	public CuisineBean getCurrentItem (int position) {
		return cardBeans.get(position);
	}

	/**
	 * 数据填充
	 *
	 * @param Beans
	 */
	public void addData (List<CuisineBean> Beans) {
		cardBeans.clear();
		for (CuisineBean cuisineBean : Beans) {
			this.cardBeans.add(cuisineBean);
		}
		notifyDataSetChanged();
	}

	class ViewHolder extends RecyclerView.ViewHolder {
		ImageView image;
		RatingBar ratingBar;
		TextView tvName;
		TextView tvPrice;
		CardView cardView;

		ViewHolder (View view) {
			super(view);
			image = view.findViewById(R.id.image);
			cardView = view.findViewById(R.id.cardView);
			ratingBar = view.findViewById(R.id.rating);
			tvName = view.findViewById(R.id.tv_name);
			tvPrice = view.findViewById(R.id.tv_price);
		}
	}
}
